package juc;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 乔巴
 * 2022-11-10 13:07
 */
public class ReentrantLockTest {
    static int size = 10;
    static  ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(size,size,1, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>());

    public static void main(String[] args) {
        for (int i = 0; i < size; i++) {
            ReentrantLock lock = new ReentrantLock();
            threadPoolExecutor.execute(()->{
                boolean b = lock.tryLock();
                if(b){
                    try {
                        lock.lock();
                        System.out.println(Thread.currentThread().getName()+"获取到了锁");
                    }catch (Exception e){

                    }finally {
                        System.out.println(Thread.currentThread().getName()+"结束运行");
                        lock.unlock();
                    }
                }

            });
        }
    }
}
